package org.looom.orglooomschool;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import org.looom.orglooomschool.service.MusicService;

public class MainActivity15 extends AppCompatActivity {
    private Button buttonPlay;
    private Button buttonPause;
    private Button buttonExit;
    private TextView textSongInfo;

    private BroadcastReceiver serviceMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // 检查 Intent 是否包含文件名信息
            String filename = intent.getStringExtra(EXTRA_FILENAME); // 使用新的 Extra Key
            if (filename != null) {
                // 更新 TextView 显示文件名
                textSongInfo.setText("当前播放: " + filename);
            }
        }
    };

    // 定义广播的 Action 字符串和 Extra Key
    public static final String ACTION_PLAYBACK_UPDATE = "org.looom.orglooomschool.PLAYBACK_UPDATE";
    public static final String EXTRA_FILENAME = "filename";

    private Intent serviceIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity15); // 请确保有对应布局文件

        buttonPlay = findViewById(R.id.button_play);
        buttonPause = findViewById(R.id.button_pause);
        buttonExit = findViewById(R.id.button_exit);
        textSongInfo = findViewById(R.id.text_song_info);

        // 播放按钮点击事件
        buttonPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                serviceIntent = new Intent(MainActivity15.this, MusicService.class);
                serviceIntent.putExtra("command", "PLAY");
                startService(serviceIntent); // 启动服务
            }
        });

        // 暂停按钮点击事件
        buttonPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                serviceIntent = new Intent(MainActivity15.this, MusicService.class);
                serviceIntent.putExtra("command", "PAUSE");
                startService(serviceIntent);
            }
        });

        // 退出按钮点击事件
        buttonExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent stopIntent = new Intent(MainActivity15.this, MusicService.class);
                stopService(stopIntent); // 停止音乐 Service
                finish(); // 关闭当前 Activity
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 创建 IntentFilter，指定要监听的 Action
        IntentFilter filter = new IntentFilter(ACTION_PLAYBACK_UPDATE);
        // 注册本地广播接收器
        LocalBroadcastManager.getInstance(this).registerReceiver(serviceMessageReceiver, filter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        // 取消注册广播接收器
        LocalBroadcastManager.getInstance(this).unregisterReceiver(serviceMessageReceiver);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 停止 Service（可选，根据你的服务是否绑定）
        if (serviceIntent != null) {
            stopService(serviceIntent);
        }
    }
}
